//
//  SCNetWorkConfig.m
//  Shuaiche
//
//  Created by 党帅 on 2019/12/24.
//  Copyright © 2019 ShuaiChe. All rights reserved.
//

#import "SCAppConfig.h"

NSString const*kSC_API_DOMAIN;
NSString const*kSC_WEB_DOMAIN;
NSString const*kSC_IM_API_ADDR;
NSString const*kSC_IM_WS_ADDR;
NSString const*kSC_IMG_DOMAIN = @"https://img.shuaiche.com";
NSString *const kSCDevEnvKey = @"SCDevEnv";

float kSCNavigationHeight;
float kSCStatusBarHeight;
float kSCScreenBottomOffset;
float kSCScreenLeftOffset;
float kSCTabBarHeight;
float kSCScreenWidth;
float kSCScreenHeight;

@implementation SCAppConfig

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static SCAppConfig *c;
    dispatch_once(&onceToken, ^{
        c = [[SCAppConfig alloc] init];
    });
    return c;
}

- (void)config {
    [self deviceConfig];
    [self networkConfig];
}

- (void)deviceConfig {
    kSCScreenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
    kSCScreenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
    kSCNavigationHeight = 44+[[UIApplication sharedApplication] statusBarFrame].size.height;
  kSCStatusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    
    if ([[UIApplication sharedApplication] statusBarFrame].size.height > 20) {
        kSCScreenBottomOffset = 34;
        kSCScreenLeftOffset = 15;
        kSCTabBarHeight = 83;
    } else {
        kSCScreenBottomOffset = 0;
        kSCScreenLeftOffset = 0;
        kSCTabBarHeight = 49;
    }
}

- (void)networkConfig {

#ifdef APP_STORE
    kSC_API_DOMAIN = @"https://api.shuaiche.com";
    kSC_WEB_DOMAIN = @"https://m.shuaiche.com";
    kSC_IM_API_ADDR = @"http://im.shuaiche.com";
    kSC_IM_WS_ADDR = @"wss://imws.shuaiche.com";
#else
    [self fetchDomainsWithEnv:APP_INNER];
#endif
}

- (void)fetchDomainsWithEnv:(NSString *)env {
    NSString *v = [[NSUserDefaults standardUserDefaults] stringForKey:kSCDevEnvKey];
    if (v && v.length) {
        if ([v isEqualToString:@"pro"]) {
            [[NSUserDefaults standardUserDefaults] setObject:env forKey:kSCDevEnvKey];
        } else {
            env = v;
        }
    }
    [self updateApiValues:env];
}

- (void)changeDomainWithEnv:(NSString *)env {
    [[NSUserDefaults standardUserDefaults] setObject:env forKey:kSCDevEnvKey];
    [self updateApiValues:env];
}

- (void)updateApiValues:(NSString *)env {
    NSDictionary *info = [self appApiDomains][env];
    kSC_API_DOMAIN = info[@"api"];
    kSC_WEB_DOMAIN = info[@"web"];
    kSC_IM_API_ADDR = info[@"imApiAddr"];
    kSC_IM_WS_ADDR = info[@"imWsAddr"];
}

- (NSString *)currentEnv {
    return [[NSUserDefaults standardUserDefaults] stringForKey:kSCDevEnvKey];
}

- (NSDictionary *)appApiDomains {
    return @{@"dev":@{@"api":@"https://api-dev.shuaiche.com",
                      @"web":@"https://m-dev.shuaiche.com",
                      @"imApiAddr":@"http://im-dev.shuaiche.com",
                      @"imWsAddr":@"wss://imws-dev.shuaiche.com"
                    },
             @"test":@{@"api":@"https://api-test.shuaiche.com",
                       @"web":@"https://m-test.shuaiche.com",
                       @"imApiAddr":@"http://im-test.shuaiche.com",
                       @"imWsAddr":@"wss://imws-test.shuaiche.com"
                    },
             @"pro":@{@"api":@"https://api.shuaiche.com",
                      @"web":@"https://m.shuaiche.com",
                      @"imApiAddr":@"http://im.shuaiche.com",
                      @"imWsAddr":@"wss://imws.shuaiche.com"
             }
    };
}

static NSString *url_common(NSString const *domain,NSString *path) {
     if ([path hasPrefix:@"https://"]) {
         return path;
     }
     
     if ([path hasPrefix:@"http://"]) {
         return [path stringByReplacingOccurrencesOfString:@"http://" withString:@"https://"];
     }
     
     if ([path hasPrefix:@"/"]) {
         return [NSString stringWithFormat:@"%@%@",domain,path];
     }
     
     return [NSString stringWithFormat:@"%@/%@",domain,path];
}

NSString*sc_url_web(NSString *path) {
    
    if ([path hasPrefix:@"/"]) {
        return [NSString stringWithFormat:@"%@%@",kSC_WEB_DOMAIN,path];
    }

    if ([path hasPrefix:@"http"]) {
        return path;
    }
    return [NSString stringWithFormat:@"http://%@",path];
}

NSString*sc_url_img(NSString *path) {
    return url_common(kSC_IMG_DOMAIN, path);
}

@end
